package com.iweb.controller;


import com.iweb.commons.ResultEntity;
import com.iweb.model.Game;

import com.iweb.service.GameService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;


/**
 * Time :2022/8/15 14:00
 * Author : kriller
 * Project : steam-malls
 */
@RestController
@RequestMapping("game")
public class GameController {
    @Resource
    private GameService gameService;

    //列出游戏
    @GetMapping("list")
    public ResultEntity<List<Game>> listGameByPage(@RequestParam int page){
        List<Game> games = gameService.selectGames(page);
        return new ResultEntity<>(2000,"获取游戏列表"+page,true,games);
    }
    //管理员列出所有游戏
    @GetMapping("admin_listGame")
    public ResultEntity<List<Game>> listAllGame(){
        List<Game> games = gameService.selectGames();
        return new ResultEntity<>(2000,"管理员获取所有游戏列表",true,games);
    }
    //按名字查找游戏
    @GetMapping("find_name")
    public ResultEntity<List<Game>> findGameByName(@RequestParam("page") Integer page, @RequestParam("gameName") String gameName){
        List<Game> games  = gameService.selectGamesByName(page,gameName);
        return new ResultEntity<>(2000,"按名字查找:"+gameName+"页码"+page,true ,games);
    }
    //按标签查找游戏
    @GetMapping("find_tag")
    public ResultEntity<List<Game>> findGameByTag(@RequestParam("page") Integer page,@RequestParam("tagName") String tagName){
        List<Game> games = gameService.selectGameByTag(page,tagName);
        return new ResultEntity<>(2000,"按标签查找:"+tagName+"页码:"+page,true,games);
    }
    //获取游戏信息
    @GetMapping("info")
    public ResultEntity<Game> findGameById(@RequestParam("gameId") Integer gameId){
        Game game  = gameService.selectById(gameId);
        return new ResultEntity<>(2000,"获取游戏信息",true,game);
    }
    //获取游戏的分类信息
    @GetMapping("tag")
    public ResultEntity<List<String>> findGameTag(@RequestParam Integer gameId){
        List<String> tags = gameService.getGameTag(gameId);
        return new ResultEntity<>(2000,"获取游戏标签",true,tags);
    }
    //增加游戏
    @PostMapping("add")
    public ResultEntity<Game> addNewGame(@RequestBody Game game){
        if (game.getGameName()==null||"".equals(game.getGameName())){
            return new ResultEntity<>(3001,"添加失败,请输入游戏名",false,null);
        }
        if (game.getGamePrice() == null){
            return new ResultEntity<>(3002,"添加失败,请输入游戏价格",false,null);
        }
        if (game.getGameState() ==null){
            game.setGameState(0);
        }
        gameService.insertNewGame(game);
        return new ResultEntity<>(2000,"增加游戏成功",true,null);
    }
    //修改游戏
    @PostMapping("modify")
    public ResultEntity<Game> modifyGame(@RequestBody Game game){
        if (game.getGameId()==null){
            return new ResultEntity<>(3001,"没有提供id",false,null);
        }
        if (!game.getGameState().equals(1)&&!game.getGameState().equals(0))
        {
            return new ResultEntity<>(3002,"无此状态",false,null);
        }
        gameService.updateGame(game);
        return new ResultEntity<>(2000,"修改游戏成功",true,null);
    }
    //删除游戏
    @PostMapping("delete")
    public ResultEntity<Game> deleteGame(@RequestBody Game game){
        Integer gameId = game.getGameId();
        gameService.deleteGameByGameId(gameId);
        return new ResultEntity<>(2000,"管理员删除游戏",true,null);
    }
}
